//
//  createScrollWheelEvent.h
//  mouseSync
//
//  Created by Chih-Hao on 2017/9/21.
//  Copyright © 2017年 Chih-Hao. All rights reserved.
//


#include <stdio.h>
#include <CoreFoundation/CoreFoundation.h>
#include <ApplicationServices/ApplicationServices.h>


extern void CoreDockSendNotification(CFStringRef /*notification*/, void * /*unknown*/);

//通过Dock通知的方式打开
void openLauchpad(void){
    
    //不知道是否是私有API ？
//    If you don't mind using a TOTALLY UNDOCUMENTED API, which might change at any point without notice:
//https://github.com/koekeishiya/yabai/issues/147
    
//    Notification    Effect
//    com.apple.expose.awake    Toggle Mission Control
//    com.apple.showdesktop.awake    Toggle Show Desktop
//    com.apple.expose.front.awake    Toggle App Exposé
//    com.apple.launchpad.toggle    Toggle Launchpad
    
    //可以通过命令打开mission control执行的方式  open -b com.apple.exposelauncher
    
    //打开launchpad/启动台 显示应用 Dock 第二个图标
//    CoreDockSendNotification(CFSTR("com.apple.launchpad.toggle"), NULL);
    
    //显示桌面，其他打开的app 四散效果，触摸板4指扩散显示桌面效果
    CoreDockSendNotification(CFSTR("com.apple.showdesktop.awake"), NULL);
    
    //系统默认快捷键  control + 向下箭头 效果，显示当前App的各个窗口
//    CoreDockSendNotification(CFSTR("com.apple.expose.front.awake"), NULL);
    
    
    //系统默认快捷键 mission control  control + 向上箭头 效果，显示所有打开并显示(没有最小化)的App小窗口
//    CoreDockSendNotification(CFSTR("com.apple.expose.awake"), NULL);
}

void createScrollWheelEventY(float x) {
    CGEventRef upEvent = CGEventCreateScrollWheelEvent(
    NULL,
    kCGScrollEventUnitPixel, 2, x*5, 0 );
    CGEventPost(kCGHIDEventTap, upEvent);
    CFRelease(upEvent);
}

void createScrollWheelEventX(int y) {
        CGEventRef upEvent = CGEventCreateScrollWheelEvent(
    NULL,
    kCGScrollEventUnitPixel, 2, 0, y*5);
    CGEventPost(kCGHIDEventTap, upEvent);
    CFRelease(upEvent);
}

void mouse_left_drag_to(float x, float y) {
    
    /**
     * create a new Quartz mouse event.
     * params:
     * @source : CGEventSourceRef
     * @mouseType : CGEventType
     * @mouseCursorPosition : CGPoint
     * @mouseButton : CGMouseButton
     */
    CGEventRef left_drag_event = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDragged, CGPointMake(x, y), 0);
    
    /**
     * post a Quartz event into the event stream at a specified location.
     * params:
     * @tap : CGEventTapLocation
     * @event : CGEventRef
     */
    CGEventPost(kCGHIDEventTap, left_drag_event);
    
    /**
     * release a Quartz event
     */
    CFRelease(left_drag_event);
}

// https://stackoverflow.com/questions/1817628/clicking-the-mouse-down-to-drag-objects-on-mac
//
// mouse_left_click_at(400.0, 200.0);          // get focus
//     usleep(10000);
//
//     mouse_left_click_down_at(400.0, 200.0);     // start dragging
//     usleep(10000);
//     mouse_left_drag_to(500.0, 400.0);           // continue dragging
//     usleep(10000);
//     mouse_left_drag_to(600.0, 500.0);
//     usleep(10000);
//     mouse_left_drag_to(700.0, 600.0);
//     usleep(10000);
//     mouse_left_drag_to(800.0, 700.0);
//     usleep(10000);
//     mouse_left_click_up_at(800.0, 700.0);       // stop dragging
//
//
// void mouse_left_click_down_at(float x, float y) {

           /**
            * create a new Quartz mouse event.
            * params:
            * @source : CGEventSourceRef
            * @mouseType : CGEventType
            * @mouseCursorPosition : CGPoint
            * @mouseButton : CGMouseButton
        */
//       CGEventRef  left_click_down_event = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, CGPointMake(x, y), 0);
//
//       /**
//        * post a Quartz event into the event stream at a specified location.
//        * params:
//        * @tap : CGEventTapLocation
//        * @event : CGEventRef
//        */
//       CGEventPost(kCGHIDEventTap, left_click_down_event);
//
//       /**
//        * release a Quartz event
//        */
//       CFRelease(left_click_down_event);
//   }
//   void mouse_left_click_up_at(float x, float y) {
//
//       /**
//        * create a new Quartz mouse event.
//        * params:
//        * @source : CGEventSourceRef
//        * @mouseType : CGEventType
//        * @mouseCursorPosition : CGPoint
//        * @mouseButton : CGMouseButton
//        */
//       CGEventRef left_click_up_event = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseUp, CGPointMake(x, y), 0);
//
//       /**
//        * post a Quartz event into the event stream at a specified location.
//        * params:
//        * @tap : CGEventTapLocation
//        * @event : CGEventRef
//        */
//       CGEventPost(kCGHIDEventTap, left_click_up_event);
//
//       /**
//        * release a Quartz event
//        */
//       CFRelease(left_click_up_event);
//   }
//
//   void mouse_left_drag_to(float x, float y) {
//
//       /**
//        * create a new Quartz mouse event.
//        * params:
//        * @source : CGEventSourceRef
//        * @mouseType : CGEventType
//        * @mouseCursorPosition : CGPoint
//        * @mouseButton : CGMouseButton
//        */
//       CGEventRef left_drag_event = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDragged, CGPointMake(x, y), 0);
//
//       /**
//        * post a Quartz event into the event stream at a specified location.
//        * params:
//        * @tap : CGEventTapLocation
//        * @event : CGEventRef
//        */
//       CGEventPost(kCGHIDEventTap, left_drag_event);
//
//       /**
//       * release a Quartz event
//       */
//       CFRelease(left_drag_event);
//   }

 

